package com.example.review.Fragment;


import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.review.R;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class DwonReFragment extends Fragment {

    @BindView(R.id.pickView)
    TextView pickView;
    @BindView(R.id.calendar)
    TextView calendar;
    private View view;
    private TimePickerView timePicker;
    private AlertDialog alertDialog;
    private CalendarView calendars;

    public DwonReFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_dwon_re, container, false);
        ButterKnife.bind(this, view);
        initData();
        return view;
    }
    public class ViewHolder {
        @BindView(R.id.nianT)
        TextView nianT;
        @BindView(R.id.xiqi)
        TextView xiqi;
        @BindView(R.id.calendarView)
        CalendarView calendarView;
        @BindView(R.id.buttonC)
        Button buttonC;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    private void initData() {
        ViewHolder viewHolder;
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_calendar, null);
        viewHolder = new ViewHolder(view);
        builder.setView(view);
        alertDialog = builder.create();
        viewHolder.calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                calendars = calendarView;
                viewHolder.nianT.setText(i+"年");
                viewHolder.xiqi.setText(i1+"月"+i2+"日");
                viewHolder.buttonC.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        calendar.setText(i+"年"+(i1+1)+"月"+i2+"日");
                        alertDialog.dismiss();
                    }
                });
            }
        });
        timePicker = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年mm月dd日");
                pickView.setText(simpleDateFormat.format(date));

            }
        }).build();
    }

    @OnClick({R.id.pickView, R.id.calendar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pickView:
                timePicker.show();
                break;
            case R.id.calendar:
                alertDialog.show();
                break;
        }
    }

}
